package com.helloae;

import java.io.File;

import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.IMap;
import com.esri.arcgis.carto.Map;
import com.esri.arcgis.carto.MapDocument;
import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.WorkspaceName;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 通过shp创建mxd文件
 * @author zhushijie
 *
 */
public class CreateMXD {
	public static void main(String[] args) throws Exception {
		//初始化AE许可
		EngineInitializer.initializeEngine();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
		
		//新建一个mxd文档，
		MapDocument mapDoc = new MapDocument();
		mapDoc.esri_new("c:\\temp\\helloMXD.mxd");
		
		//获得Map对象。
		//上面的esri_new方法会创建一个空Map
		//如果没有特殊要求，比如需要多个Data Frame，那么直接使用即可
		IMap myMap = new Map();
		myMap = mapDoc.getMap(0);
		
		//加载shapefile文件
		ShapefileWorkspaceFactory swFactory = new ShapefileWorkspaceFactory();
		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		IFeatureWorkspace fWorkspace = (IFeatureWorkspace)swFactory.openFromFile(ecliseWorkspace+"HelloAE"+File.separator+"DataSet"+File.separator+"other", 0);
		IFeatureClass fClass = fWorkspace.openFeatureClass("helloPoint");
		
		FeatureLayer fLayer = new FeatureLayer();
		fLayer.setFeatureClassByRef(fClass);
		//给图层起个名字
		fLayer.setName(fClass.getAliasName());
		myMap.addLayer(fLayer);
		
		//给map起个名字
		myMap.setName("我的地图");
		
		//保存mxd
		mapDoc.save(false, false);
		ao.shutdown();
		System.out.println("Congratulations!");
	}

}
